package com.binarysoft.service.product;

import java.util.Collection;
import java.util.Date;

import org.apache.ibatis.session.SqlSession;

import com.binarysoft.service.id.IDService;
import com.github.pagehelper.PageHelper;
import common.framework.dsb.AbstractServiceBean;
import common.framework.dsb.annotation.DSBLog;
import common.framework.dsb.annotation.DSBService;
import common.framework.dsb.annotation.DSBSql;
import common.framework.dsb.service.ServiceContext;

@DSBService(name = "DSB/ProductService", startup = true)
public class ProductServiceBean extends AbstractServiceBean implements ProductService {

	@Override
	public void start(ServiceContext serviceContext) throws Exception {
		super.start(serviceContext);

	}

	@Override
	public void refresh() throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void close() throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	@DSBSql(environment = "mysql_dev")
	public Collection<Product> getAllProducts() throws Exception {
		SqlSession sqlSession = this.getSqlSession();
		ProductDAO dao = sqlSession.getMapper(ProductDAO.class);
		return dao.selectAll();
	}

	@Override
	@DSBSql(environment = "mysql_dev")
	public Collection<Product> getAllProducts(int pageNumber, int pageSize) throws Exception {
		SqlSession sqlSession = this.getSqlSession();
		ProductDAO dao = sqlSession.getMapper(ProductDAO.class);
		PageHelper.startPage(pageNumber, pageSize);
		return dao.selectAll();
	}

	@Override
	@DSBSql(environment = "mysql_dev")
	public Product getProduct(long productID) throws Exception {
		SqlSession sqlSession = this.getSqlSession();
		ProductDAO dao = sqlSession.getMapper(ProductDAO.class);
		return dao.selectByPrimaryKey(productID);
	}

	@Override
	@DSBLog(log = "file")
	@DSBSql(environment = "mysql_dev")
	public void addProduct(Product product) throws Exception {
		IDService idService = this.lookup("DSB/IDService");
		product.setId(idService.nextID());
		product.setCreationTime(new Date());

		SqlSession sqlSession = this.getSqlSession();
		ProductDAO dao = sqlSession.getMapper(ProductDAO.class);
		dao.insert(product);
	}

	@Override
	@DSBSql(environment = "mysql_dev", batch = true)
	public void addProduct(Collection<Product> products) throws Exception {
		batchInsert(products);
	}

	@Override
	@DSBLog(log = "file")
	@DSBSql(environment = "mysql_dev")
	public void deleteProduct(long productID) throws Exception {
		SqlSession sqlSession = this.getSqlSession();
		ProductDAO dao = sqlSession.getMapper(ProductDAO.class);
		dao.deleteByPrimaryKey(productID);
	}

	@Override
	@DSBSql(environment = "mysql_dev")
	public void updateProduct(Product product) throws Exception {
		SqlSession sqlSession = this.getSqlSession();
		ProductDAO dao = sqlSession.getMapper(ProductDAO.class);
		dao.updateByPrimaryKeySelective(product);
		// dao.updateByPrimaryKey(product);
	}

	@Override
	@DSBSql(environment = "mysql_dev", batch = true)
	public void updateProduct(Collection<Product> products) throws Exception {
		batchUpdate(products);
	}

	private void batchInsert(Collection<Product> records) throws Exception {
		IDService idService = this.lookup("DSB/IDService");
		SqlSession sqlSession = this.getSqlSession();
		ProductDAO dao = sqlSession.getMapper(ProductDAO.class);
		int count = 0;
		int batch_size = 100;
		// pre-process
		for (Product record : records) {
			// insert
			record.setId(idService.nextID());
			record.setCreationTime(new Date());
			dao.insert(record);
			count++;
			// 每100条记录提交一次
			if (count == batch_size) {
				// commit
				sqlSession.commit();
				count = 0;
			}
		}
		if (count > 0) {
			// last commit
			sqlSession.commit();
		}
	}

	private void batchUpdate(Collection<Product> records) throws Exception {
		SqlSession sqlSession = this.getSqlSession();
		ProductDAO dao = sqlSession.getMapper(ProductDAO.class);
		int count = 0;
		int batch_size = 100;
		// pre-process
		for (Product record : records) {
			// insert
			dao.updateByPrimaryKey(record);
			count++;
			// 每100条记录提交一次
			if (count == batch_size) {
				// commit
				sqlSession.commit();
				count = 0;
			}
		}
		if (count > 0) {
			// last commit
			sqlSession.commit();
		}
	}
}
